package com.cn.auth.utils;


import com.cn.auth.constant.ResponseCodeFace;
import com.cn.auth.enums.ResponseCodeEnum;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wuchengrui
 * @Description: 全局错误码获取和注册类
 * @date 2021/5/14 16:15
 */
public class GlobalErrorCodeUtil {

    private static Map<Integer , ResponseCodeFace> globalMap = new ConcurrentHashMap<>();

    public static void accessRegistry(ResponseCodeFace responseCodeFace){

        globalMap.put(responseCodeFace.getCode(),responseCodeFace);

    }

    public static ResponseCodeFace getCodeFace(int code){
        ResponseCodeFace responseCodeFace = globalMap.get(code);
        if(responseCodeFace == null){
            return ResponseCodeEnum.FAIL;
        }

        return responseCodeFace;
    }

    public static ResponseCodeFace getCodeFace(ResponseCodeFace face){

        return getCodeFace(face.getCode());
    }

    /**
     * 将错误码改成map
     * */
    public static Map<Integer,String> getGlobalMap(){
        Map<Integer,String> map = new HashMap<>();
        globalMap.values().forEach(face -> {
            map.put(face.getCode(),face.getDoc());
        });
        return map;


    }








}
